﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ERPStore.Tests;
using ERPStore.Extensions;

using NUnit.Framework;

namespace ERPStore.Exporter.Tests.Controllers
{
	[TestFixture]
	public class ExporterControllerTests : TestBase
	{
		[SetUp]
		public override void Initialize()
		{
			base.Initialize();
		}

		[Test]
		public void Redirect()
		{
			var httpContext = GetHttpContext();
			var mockHttpRequest = Moq.Mock.Get(httpContext.Request);
			mockHttpRequest.Setup(req => req.UrlReferrer).Returns(new Uri("http://www.google.fr/?q=xbox"));
			var exporterController = CreateController<ERPStore.Web.Exporter.Controllers.ExporterController>();

			var exporterService = GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.Exporter.Services.ExporterService>();
			var catalogService = GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.Services.ICatalogService>();
			var leadSourceService = GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.Services.ILeadSourceRegistrationService>();


			var product = catalogService.GetProductByCode("xbox360");
			var leadSource = leadSourceService.List.FirstOrDefault(i => i.Id == "D36E99D7-CA2E-439d-B594-442D582FE118");
			var key = exporterService.BuildEncryptedProductKey(leadSource, product);

			var view = exporterController.TrackerRedirect(key) as System.Web.Mvc.ViewResult;

			var @event = view.Model as ERPStore.Events.TrackerEvent;

			Assert.IsNotNull(@event);

			Assert.AreEqual(view.ViewName, "ExporterRedirect");

			Assert.AreEqual(view.ViewBag.SourceName, "gshopping");
			Assert.AreEqual(view.ViewBag.SourceId , "D36E99D7-CA2E-439d-B594-442D582FE118");
			Assert.AreEqual(view.ViewBag.Medium, "cpc");
			Assert.AreEqual(view.ViewBag.ProductCode, "xbox360");
		}

	}
}
